package com.rathgael.hoteldata;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.rathgael.hoteldata.domain.Hotel;

import au.com.bytecode.opencsv.CSVReader;
import au.com.bytecode.opencsv.CSVWriter;

public class HotelCSVFileGenerator {

    public static List<Hotel> loadHotelList(File source) throws IOException {
        List<Hotel> hotels = new ArrayList<Hotel>();

        CSVReader reader = new CSVReader(new FileReader(source), '\t', '\"', 1);
        String[] nextLine;

        while ((nextLine = reader.readNext()) != null) {
            Hotel hotel = new Hotel();

            hotel.setName(nextLine[0]);
            hotel.setAddress1(nextLine[1]);
            hotel.setAddress2(nextLine[2]);
            hotel.setTown(nextLine[3]);
            hotel.setPostcode(nextLine[4]);
            hotel.setCountyCode(nextLine[5]);
            hotel.setTelephone(nextLine[6]);
            hotel.setFax(nextLine[7]);
            hotel.setOwner(nextLine[8]);
            hotel.setBeds(nextLine[9]);
            hotel.setStars(nextLine[10]);
            hotel.setEmail(nextLine[11]);
            hotel.setWebsite(nextLine[12]);
            hotel.setContactSalutation(nextLine[13]);
            hotel.setContactFirstNmae(nextLine[14]);
            hotel.setContactLSurname(nextLine[15]);

            hotels.add(hotel);
        }

        return hotels;
    }

    private static void writeUpdatedHotelList(List<Hotel> hotelList) throws IOException {
        CSVWriter writer = new CSVWriter(new FileWriter(Settings.HOTEL_CSV_TARGET), ',');

        String[] headers = new String[] { "LNAME", "ADD1", "ADD2", "TOWN", "PC", "CTY", "TELE", "FAX", "OWNER", "BEDS", "STARS", "E-MAIL", "WEB", "SALUT", "FIRST", "SURNAME", "FULLNAME", "FAXMAIL" };

        writer.writeNext(headers);
        for (Hotel hotel : hotelList) {
            if (hotel.getFaxMail() != null) {
                String[] line = new String[19];

                line[0] = hotel.getName();
                line[1] = hotel.getAddress1();
                line[2] = hotel.getAddress2();
                line[3] = hotel.getTown();
                line[4] = hotel.getPostcode();
                line[5] = hotel.getCounty();
                line[6] = hotel.getTelephone();
                line[7] = hotel.getFax();
                line[8] = hotel.getOwner();
                line[9] = hotel.getBeds();
                line[10] = hotel.getStars();
                line[11] = hotel.getEmail();
                line[12] = hotel.getWebsite();
                line[13] = hotel.getContactSalutation();
                line[14] = hotel.getContactFirstNmae();
                line[15] = hotel.getContactLSurname();
                line[16] = hotel.getFullContactName();
                line[17] = hotel.getFaxMail();

                writer.writeNext(line);
            }
        }

        writer.close();
    }

    private static void analyseBedCount(List<Hotel> hotelList) {
        int noBedInfo = 0;

        for (Hotel hotel : hotelList) {
            if (hotel.getBeds() == null) {
                noBedInfo++;
            }
            else {
                new Integer(hotel.getBeds());
            }
        }

        System.out.println(noBedInfo + " hotels have no inofrmation about bed count");
    }

    private static void analyseFaxNumbers(List<Hotel> hotelList) {
        Map<Character, Integer> charCount = new HashMap<Character, Integer>();
        boolean fucked = false;
        for (Hotel hotel : hotelList) {
            String trimmedFax = hotel.getFax().trim().replace(".", "");

            char[] charArray = trimmedFax.toCharArray();

            for (char c : charArray) {
                charCount.put(c, charCount.get(c) == null ? 1 : charCount.get(c) + 1);
                if (!Character.isDigit(c)) {
                    fucked = true;
                }
            }

            if (fucked) {
                System.out.println(hotel.getFax());
            }
            fucked = false;
        }

        System.out.println("Complete list of characters");
        for (Entry<Character, Integer> e : charCount.entrySet()) {
            System.out.println(e.getKey() + ", " + e.getValue());
        }
        System.out.println("End list of characters");

        Map<Integer, Integer> faxLength = new HashMap<Integer, Integer>();
        for (Hotel hotel : hotelList) {
            if (hotel.getFaxMail() != null) {
                int length = hotel.getFaxMail().length();
                faxLength.put(length, faxLength.get(length) == null ? 1 : faxLength.get(length) + 1);

                if (length != 27) {
                    System.out.println("Potential problem with number " + hotel.getFaxMail() + " from " + hotel.getFax());
                }
            }
        }

        System.out.println("Complete list of fax number lengths");
        for (Entry<Integer, Integer> e : faxLength.entrySet()) {
            System.out.println(e.getKey() + ", " + e.getValue());
        }
        System.out.println("End list of fax number lengths");
    }

    public static void main(String[] args) throws IOException {
        List<Hotel> hotelList = HotelCSVFileGenerator.loadHotelList(new File(Settings.HOTEL_TAB_SOURCE));
        analyseFaxNumbers(hotelList);
        analyseBedCount(hotelList);
        HotelCSVFileGenerator.writeUpdatedHotelList(hotelList);
    }

}
